﻿using System;
using System.Activities.Presentation.PropertyEditing;
using System.Collections.Generic;
using System.Windows;
using System.Xml.Linq;

namespace ApocDev.TownsApi.XmlFiles
{
    public class TerrainValueEditor : ExtendedPropertyValueEditor
    {
        
    }

    public class TerrainXml : List<Terrain>
    {
        private readonly string _filePath;
        public TerrainXml(string path)
        {
            _filePath = path;

            XElement root = XElement.Load(path);
            var terrains = root/*.Element("terrain")*/.Elements();
            
            foreach (var ele in terrains)
            {
                Add(new Terrain(ele));
            }
        }
        
        public void Save()
        {
            XElement root = new XElement("terrain");
            foreach (var terrain in this)
            {
                root.Add(terrain.Save());
            }
            root.Save(_filePath);
        }
    }

    public class Terrain
    {
        public override string ToString()
        {
            if (String.IsNullOrWhiteSpace(Name))
                return ID;
            return Name + " (" + ID + ")";
        }
        public string ID { get; set; }
        public Terrain(XElement xml)
        {
            ID = xml.Name.ToString();
            // ReSharper disable PossibleNullReferenceException

            foreach (var e in xml.Elements())
            {
                switch (e.Name.LocalName.ToLower())
                {
                    case "name":
                        Name = e.Value;
                        break;
                    case "mineturns":
                        MineTurns = int.Parse(e.Value);
                        break;
                    case "drop":
                        _dropItemId = e.Value;
                        break;
                    case "ladderitem":
                        _ladderItemId = e.Value;
                        break;
                    case "droppct":
                        DropPercent = int.Parse(e.Value);
                        break;
                    case "action":
                        _actionId = e.Value;
                        break;
                    case"canbefilled":
                        CanBeFilled = e.Value.ToLower() == "true";
                        break;
                    case "group":
                        Group = e.Value;
                        break;
                    case "blocky":
                        Blocky = e.Value.ToLower() == "true";
                        break;
                    default:
                        MessageBox.Show("Unknown element in terrain.xml: " + e.Name);    
                        break;
                }
            }
        }

        private string _dropItemId, _ladderItemId, _actionId;

        public string Name { get; set; }
        public int MineTurns { get; set; }
        public Item Drop { get { return ModFiles.GetItem(_dropItemId); } set { _dropItemId = value.ID; } }
        public int DropPercent { get; set; }
        public Item LadderItem { get { return ModFiles.GetItem(_ladderItemId); } set { _ladderItemId = value.ID; } }
        public Action Action { get { return ModFiles.GetAction(_actionId); } set { _actionId = value.ID; } }
        public bool CanBeFilled { get; set; }
        public string Group { get; set; }
        public bool Blocky { get; set; }
        public XElement Save()
        {
            // These are the required ones. The rest are optional!
            var root = new XElement(ID,
                new XElement("name", Name),
                new XElement("mineTurns", MineTurns));

            if (MineTurns > 0)
                root.Add(new XElement("mineTurns", MineTurns));
            if(Drop != null)
                root.Add(new XElement("drop", Drop.ID));
            if (DropPercent > 0)
                root.Add(new XElement("dropPCT", DropPercent));
            if (LadderItem != null)
                root.Add(new XElement("ladderItem", LadderItem.ID));
            if (Action != null)
                root.Add(new XElement("action", Action.ID));
            if (CanBeFilled)
                root.Add(new XElement("canBeFilled", "true"));
            if (!string.IsNullOrWhiteSpace(Group))
                root.Add(new XElement("group", Group));

            return root;
        }
    }
}
